package com.example.learn.hwod;

import java.util.ArrayList;
import java.util.Scanner;

public class _25猜数字 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] guesses = new String[n];
        String[] results = new String[n];

        for (int i = 0; i < n; i++) {
            guesses[i] = sc.next();
            results[i] = sc.next();
        }
        ArrayList<String> answers = new ArrayList<>();
        for (int num = 0; num <= 9999; num++) {
            String answer = String.format("%04d", num); // 补足前导0, 使num变为四位, 当作一个可能的answer

            // 和每一个guess比较, 对比结果是否满足result
            int i = 0;
            for (; i < guesses.length; i++) {
                if (!results[i].equals(getResult(answer, guesses[i]))) {
                    break;
                }
            }

            // 只有全部满足时, 当前answer才是一个符合要求的解
            if (i == guesses.length) {
                answers.add(answer);
            }
        }

        if (answers.size() != 1) {
            // 不存在唯一解，则答案不确定
            System.out.println("NA");
        } else {
            System.out.println(answers.get(0));
        }


    }

    public static String getResult(String answer, String guess) {
        int X = 0;
        int Y = 0;

        ArrayList<Character> guessList = new ArrayList<>();
        ArrayList<Character> answerList = new ArrayList<>();

        for (int i = 0; i < 4; i++) {
            if (guess.charAt(i) == answer.charAt(i)) {
                X++; // 数字正确且位置正确
            } else {
                guessList.add(guess.charAt(i));
                answerList.add(answer.charAt(i));
            }
        }

        for (Character c : guessList) {
            if (answerList.remove(c)) {
                Y++; // 数字正确而位置不对的数的个数
            }
        }

        return X + "A" + Y + "B";
    }

}
